cap log close
log using "${log}/analysis_replication_maintext.log", replace
set more off
set matsize 800

version 14
set scheme s2color


/*************************************************************************\
|    Authors:         Hampole, Truffa, Wong		                          |
|    Date:            January 2026                                        |
|    Purpose:         Replication main analysis        					  |
\*************************************************************************/


* Figures
local p1 = 1 //Figure 1: Distribution of Female Share across Sections by Graduating Cohort
local p2 = 1 //Figure 2: Representation in the Corporate Pipeline Among MBA Graduates in the First 15 Years Post-Graduation by Gender 
local p3 = 1 //Figure 3: Probability of Holding a Senior-Level Management Position by Gender
local p4 = 1 //Figure 4: Five-Year Transition Probabilities for First-Level Managers by Gender
local p5 = 1 //Figure 5: Probability of Senior-Level Manager
local p6 = 1 //Figure 6: Effect of Female Peers on Senior-Level Management Positions By Year Since Graduation 
local p7 = 1 //Figure 7: Effect of Female Peers on Female-Friendly Firms by Year Since Graduation
local p8 = 1 //Figure 8: MBA Peer Support (Survey)
local p9 = 1 //Figure 9: Effect of Female Peers on MBA Peer Support (Survey)


* Tables
local p10 = 1 //Table 1: Match Statistics
local p11 = 1 //Table 2: Randomization Test - Guryan et al., 2009 and Caeyers and Fafchamps, 2024
local p12 = 1 //Table 3: Effect of Female Peers on Senior Management 
local p13 = 1 //Table 4: Effect of Female Peers on Compensation
local p14 = 1 //Table 5: Effect of Female Peers on Probability of Senior Management in Female-Friendly Firms 
local p15 = 1 //Table 6: Effects of Female Peers on the Work Impacts of Children (Survey)
local p16 = 1 //Table 7: Probability of Entering the Same Firm


* Figure 1: Distribution of Female Share across Sections by Graduating Cohort
if `p1'==1{

use "${data}/raw/LI_workhistory_yearly_educ_gender_fake.dta",clear
drop if SectionGroupNumber==.
bys section_id: keep if _n==1

*gender
graph box femaleshare_section,  over(Class,label(labsize(vsmall))) plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) ///
				ytitle("Share of Female MBA per Section") yscale(range(0.2 0.7)) ylabel(0.2 (0.1) 0.7) scheme(s2mono) 
graph export "${output}/graphs/Figure_1a.eps",  replace	

graph bar femaleshare_section,  over(SectionGroupNumber, sort(1) label( nolabels)) over(Class,label(labsize(vsmall)) ) ///
				plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white)  ytitle("Share of Female MBA per Section") scheme(s2mono) 
graph export "${output}/graphs/Figure_1b.eps",  replace
	
}

* Figure 2: Representation in the Corporate Pipeline Among MBA Graduates in the First 15 Years Post-Graduation by Gender
if `p2'==1{
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear


keep if year_since_grad==15


foreach y in ever_any_mgr_v3 ever_mgr_v3 ever_vp_dir_v3 ever_svp_v3 ever_csuite_v3{

	matrix `y' = J(1,3,.)
	matrix coln `y' = mean ll95 ul95
	reg `y' female
	matrix `y'[1,1] = _b[_cons]+_b[female], _b[_cons]+_b[female]-1.96*_se[female], _b[_cons]+_b[female]+1.96*_se[female]
	
}

		
collapse ever_any_mgr_v3 ever_vp_dir_v3 ever_svp_v3 ever_csuite_v3,by(female)
drop if female==.
gen id = 1

reshape long ever_@_v3, i(id female) j(mgr_cat) string


gen mgr_cat_order = 1 if mgr_cat=="any_mgr"
replace mgr_cat_order = 2 if mgr_cat=="vp_dir"
replace mgr_cat_order = 3 if mgr_cat=="svp"
replace mgr_cat_order = 4 if mgr_cat=="csuite"

sort mgr_cat_order female
gen mgr_cat_female = female
replace mgr_cat_female = female + 3 if mgr_cat_order==2
replace mgr_cat_female = female + 6 if mgr_cat_order==3
replace mgr_cat_female = female + 9 if mgr_cat_order==4

gen ll95 = . 
replace ll95 = ever_any_mgr_v3[1,2] if mgr_cat_order==1 & female==1
replace ll95 = ever_vp_dir_v3[1,2] if mgr_cat_order==2 & female==1
replace ll95 = ever_svp_v3[1,2] if mgr_cat_order==3 & female==1
replace ll95 = ever_csuite_v3[1,2] if mgr_cat_order==4  & female==1

gen ul95 = . 
replace ul95 = ever_any_mgr_v3[1,3] if mgr_cat_order==1 & female==1
replace ul95 = ever_vp_dir_v3[1,3] if mgr_cat_order==2 & female==1
replace ul95 = ever_svp_v3[1,3] if mgr_cat_order==3 & female==1
replace ul95 = ever_csuite_v3[1,3] if mgr_cat_order==4  & female==1


twoway (bar ever__v3 mgr_cat_female if female==0) ///
       (bar ever__v3 mgr_cat_female if female==1) ///
       (rcap ul95 ll95 mgr_cat_female), ///
	   legend(row(1) order(1 "Male" 2 "Female") ) ///
       xlabel( 0.5 "Any Management" 3.5 "VP or Director" 6.5 "SVP" 9.5 "C-Suite", noticks) ///
       ytitle("Probability of Ever Holding Position") xtitle("") plotregion(fcolor(white)) ///
	   graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) scheme(s2mono) 
graph export "${output}/graphs/Figure_2.eps",  replace
}

* Figure 3: Probability of Holding a Senior-Level Management Position by Gender
if `p3'==1{
	
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
//keep if Class >=2000 & Class <=2015

local lastyear = 15
foreach y in  mgr2_v3 {

	if "`y'"=="mgr2_v3"{
		local name "Senior-Level Manager"
		local label ""
		local elabel ""
	}
	binscatter `y' year_since_grad if year_since_grad>=1 & year_since_grad<=`lastyear', by(female) linetype(none) discrete ///
		plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) xlabel(1(1)`lastyear') `label' legend(order(1 "Male" 2 "Female")) ///
		xtitle("Years since MBA graduation") ytitle("`name'") lcolors(black*0.3 black) mcolors(black*.3 black) scheme(stmono1) plotregion(lstyle(none))
	graph export "${output}/graphs/Figure_3.eps",  replace
}
}

* Figure 4: Five-Year Transition Probabilities for First-Level Managers by Gender
if `p4'==1{

use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
keep if year_since_grad <=15 & year_since_grad>=1

keep linkedin_id female ihs_stars_med year_since_grad any_mgr_v3 mgr_v3 vp_dir_v3 svp_v3 csuite_v3 employed
gen no_mgr_v3 = 1-any_mgr_v3

gen mgr_cat = 0 if no_mgr_v3==1
replace mgr_cat = 1 if mgr_v3==1
replace mgr_cat = 2 if vp_dir_v3==1
replace mgr_cat = 3 if svp_v3==1
replace mgr_cat = 4 if csuite_v3==1
replace mgr_cat = . if mgr_v3==. &vp_dir_v3==. & svp_v3==. & csuite_v3==. 
replace mgr_cat = -1 if employed==0

sort linkedin_id year
by linkedin_id: gen mgr_cat_next = mgr_cat[_n+5] if  mgr_cat[_n+5] !=.

tab mgr_cat mgr_cat_next, row

collapse (count) n=year, by(mgr_cat mgr_cat_next female)
drop if mgr_cat_next==. | mgr_cat==.

bys mgr_cat female: egen n_total = total(n)
gen frac = n/n_total


gen up_down = 0 if mgr_cat_next==0
replace up_down = 1 if mgr_cat_next<mgr_cat & mgr_cat_next!=0
replace up_down = 2 if mgr_cat_next==mgr_cat
replace up_down = 3 if mgr_cat_next>mgr_cat
replace up_down = -1 if mgr_cat_next==-1

collapse (sum) n (first) n_total ,by(female mgr_cat up_down)
gen frac = n/n_total

lab def up_down -1 "Nonemployed" 0 "Non-Manager" 1 "Lower Position" 2 "Same Position" 3 "Higher Position",replace
lab val up_down up_down

lab def female 0 "Male" 1 "Female"
lab val female female
	
* low level managers
local i =1
graph bar (asis) frac if mgr_cat==`i' , asyvars  bargap(5)  over(up_down, relabel(1 "Nonemployed" 2 "Non-Manager" 3 "First Manager" 4 "Senior Manager")) over(female)  ///
	legend(rows(2) size(small) position(6))  plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) ///
	 blabel(bar, format(%4.2f) size(small))  plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) ///
	 label  ///
	 ytitle("Probability of Being in Given Position") scheme(s2mono) 
graph export "${output}/graphs/Figure_4.eps",  replace	
	
}

* Figure 5: Probability of Senior-Level Manager
if `p5'==1{

use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
//keep if Class >=2000 & Class <=2015

local lastyear = 15

local missings pre_any_mgr_v3 pre_mgr2_v3 pre_founder ///
		pre_i_fin pre_i_cons pre_i_good pre_i_hlth pre_i_tech pre_i_oth ///
		pre_small pre_medium pre_big pre_top_100_firm pre_size_LI ///
		ivy top20 pre_exp ///
		pre_mgr_v3 pre_vp_dir_v3 pre_svp_v3 pre_csuite_v3 ///
		pre_f_IT pre_f_acct pre_f_adm pre_f_cons pre_f_cus ///
		pre_f_fin pre_f_mgmt pre_f_HR pre_f_legal pre_f_mkt ///
		pre_f_opn pre_f_PR pre_f_prod pre_f_RD pre_f_sales pre_f_strat ///
		pre_PL pre_PL2 

		
foreach var of local missings {
		
		*create missing indicator 
		gen `var'_m = (`var'==.)
		
		*replace missing values with zero
		replace `var'=99 if `var'==.
    }

reg femaleshare i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
		 if year_since_grad>=1 & year_since_grad<=`lastyear', cluster(section_id)
	predict femaleshare_res, residuals
	
	sum femaleshare if female==0
	replace femaleshare_res = femaleshare_res+`r(mean)' if female==0
	
	sum femaleshare if female==1
	replace femaleshare_res = femaleshare_res+`r(mean)' if female==1

foreach y in mgr2_v3 { 
	
		binscatter `y' femaleshare if year_since_grad>=1 & year_since_grad<=`lastyear', by(female) absorb(Class) ///
	plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white)  xtitle("Female Share of Section Peers") ///
	legend(order(1 "Male" 2 "Female")) ytitle("`name'") lcolors(black*0.3 black) mcolors(black*0.3 black) msymbol(O T) nquantiles(10) scheme(stmono1) plotregion(lstyle(none))
	graph export "${output}/graphs/Figure_5a.eps",  replace
	
	*** Binscatter wrt female share 
	reg `y' i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
		 if year_since_grad>=1 & year_since_grad<=`lastyear', cluster(section_id) 
	predict `y'_res, residuals
	
	sum `y' if female==0
	replace `y'_res = `y'_res+`r(mean)' if female==0
	
	sum `y' if female==1
	replace `y'_res = `y'_res+`r(mean)' if female==1
	
	binscatter `y'_res femaleshare_res if year_since_grad>=1 & year_since_grad<=`lastyear', by(female) absorb(Class) ///
	plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white)  xtitle("Female Share of Section Peers")   ///
	legend(order(1 "Male" 2 "Female")) ytitle("`name'")  lcolors(black*0.3 black) mcolors(black*0.3 black) msymbol(O T) nquantiles(10) scheme(stmono1) plotregion(lstyle(none))
	graph export "${output}/graphs/Figure_5b.eps",  replace		
}


}

* Figure 6: Effect of Female Peers on Senior-Level Management Positions By Year Since Graduation
if `p6'==1{

use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
	
local missings pre_any_mgr_v3 pre_mgr2_v3 pre_founder ///
		pre_i_fin pre_i_cons pre_i_good pre_i_hlth pre_i_tech pre_i_oth ///
		pre_small pre_medium pre_big pre_top_100_firm pre_size_LI ///
		ivy top20 pre_exp ///
		pre_mgr_v3 pre_vp_dir_v3 pre_svp_v3 pre_csuite_v3 ///
		pre_f_IT pre_f_acct pre_f_adm pre_f_cons pre_f_cus ///
		pre_f_fin pre_f_mgmt pre_f_HR pre_f_legal pre_f_mkt ///
		pre_f_opn pre_f_PR pre_f_prod pre_f_RD pre_f_sales pre_f_strat ///
		pre_PL pre_PL2 

		
foreach var of local missings {
		
		*create missing indicator 
		gen `var'_m = (`var'==.)
		
		*replace missing values with zero
		replace `var'=99 if `var'==.
    }
	


	
keep if year_since_grad>=1 & year_since_grad<=15

sort linkedin_id year_since_grad


sort linkedin_id year_since_grad
foreach x in ihs_stars_med ///
	{
	gen temp = `x' if year_since_grad>=1
	by linkedin_id: gen ever_`x' = sum(temp)
	replace ever_`x' = 1 if ever_`x'>1 & ever_`x' !=.
	replace ever_`x' = . if year_since_grad < 1
	drop temp
}

*generate manager unconditional on working		
gen mgr2_v3_cond = mgr2_v3
replace mgr2_v3_cond = . if (employed==0 | employed==.) & mgr2_v3==0

gen i_male = (i_fin==1 | i_cons==1 | i_tech==1)
replace i_male =. if (i_good==. & i_hlth==. & i_fin==. & i_cons==. & i_tech==. & i_oth==.)

gen i_female = 1- i_male
replace i_female = 0 if i_oth==1

gen i_male_mgr2_v3 = i_male==1 & mgr2_v3==1
replace  i_male_mgr2_v3  = . if i_male==. | mgr2_v3==.
gen i_female_mgr2_v3 = i_female==1 & mgr2_v3==1
replace  i_female_mgr2_v3  = . if i_female==. | mgr2_v3==.

gen i_male_mgr2_v3_c  = i_male_mgr2_v3 
replace i_male_mgr2_v3_c = . if mgr2_v3 !=1

*industries 
gen i_categorical = 1 if i_fin==1
replace i_categorical = 2 if i_cons==1
replace i_categorical = 3 if i_good==1
replace i_categorical = 4 if i_hlth==1
replace i_categorical = 5 if i_tech==1
replace i_categorical = 6 if i_oth==1

lab def i_categorical 1 "Finance" 2 "Consulting" 3 "Cons. Good" 4 "Healthcare" 5 "Technology" 6 "Other"
lab value i_categorical i_categorical

*functions
encode function_broad, gen(f_broad_categorical)
ta function_broad, gen(function_broad)


rename gd_fem_share_senior_manager gd_fem_share_sr_mgr
rename gd_fem_pct_d_totalpay gd_fem_pct_totpay
rename gd_sr_mgr_fem_pct_d_totalpay gd_sr_mgr_fem_pct_totpay

gen comp_gap = - gd_fem_pct_d_basepay
gen sr_comp_gap = - gd_sr_mgr_fem_pct_d_basepay


*generate lee bounds on ihs_stars
gen ihs_stars_l = ihs_stars
replace ihs_stars_l = 1.1 if ihs_stars==.
gen ihs_stars_u = ihs_stars
replace ihs_stars_u = 4.9 if ihs_stars==.

gen ihs_stars_m = (ihs_stars==.)
gen gd_m = (gd_fem_share_emp==. & gd_fem_share_sr_mgr==. & gd_basepay==. & comp_gap==. & sr_comp_gap==.)
gen fgb_rating_m = (fgb_rating==.)

sum ihs_ratings_count
replace ihs_ratings_count = `r(min)' if ihs_ratings_count==. & ihs_stars==.

*************************************
* Manager and Family Friendly Firms *
*************************************
local fam_vars ihs_stars_med

foreach var in `fam_vars'{
	gen `var'_mgr = (`var'==1 & mgr2_v3==1)
	replace `var'_mgr=. if `var'==. | mgr2_v3==.
}	

gen ihs_stars_no_med_mgr = (ihs_stars_med==0 & mgr2_v3==1)
replace ihs_stars_no_med_mgr=. if ihs_stars_med==. | mgr2_v3==.


*create manager + external
preserve
drop if year_since_grad==1

collapse (max)external, by(linkedin_id firmnum mgr2_v3)
drop if firmnum==.
gen external_mgr = (external==1 & mgr2_v3==1)
replace external_mgr=. if external==. | mgr2_v3==.
gen internal_mgr = (mgr2_v3==1 & external_mgr==0)
replace internal_mgr=. if mgr2_v3==. | external_mgr==.
tempfile ext_int
save `ext_int', replace 
restore

merge m:1 linkedin_id firmnum mgr2_v3 using `ext_int'

*create total number of years in senior manager positions
bys linkedin_id: egen mgr2_v3_tot_years = total(mgr2_v3)

*rename variables with too long names
rename gd_sr_mgr_fem_pct_d_basepay gd_sr_mgr_fem_pct_d_base

gen gd_i_bonuspay = gd_i_totalpay - gd_i_basepay

gen log_gd_i_totalpay = ln(gd_i_totalpay+1)
gen log_gd_i_bonuspay = ln(gd_i_bonuspay+1)
gen log_gd_i_basepay = ln(gd_i_basepay+1)

*create manager + family friendly index + male dom
gen a_m_fam_fr_male_mgr2_v3 = (ihs_stars_med==1 & mgr2_v3==1 & i_male==1)
replace a_m_fam_fr_male_mgr2_v3=. if ihs_stars_med==. | mgr2_v3==. | i_male==.

gen b_m_fam_fr_male_mgr2_v3 = (ihs_stars_med==0 & mgr2_v3==1 & i_male==1)
replace b_m_fam_fr_male_mgr2_v3=. if ihs_stars_med==. | mgr2_v3==. | i_male==.

gen a_m_fam_fr_female_mgr2_v3 = (ihs_stars_med==1 & mgr2_v3==1 & i_female==1)
replace a_m_fam_fr_female_mgr2_v3=. if ihs_stars_med==. | mgr2_v3==. | i_female==.

gen b_m_fam_fr_female_mgr2_v3 = (ihs_stars_med==0 & mgr2_v3==1 & i_female==1)
replace b_m_fam_fr_female_mgr2_v3=. if ihs_stars_med==. | mgr2_v3==. | i_female==.
	
preserve
bys section_id:keep if _n==1
sum femaleshare,d
restore
local p25 = r(p25)
local p75 = r(p75)
local p1 = r(p1)
local p99 = r(p99)
local p10 = r(p10)
local p90 = r(p90)

eststo clear
matrix results1 = J(15,3,.)
matrix results2 = J(15,3,.)
matrix results3 = J(15,3,.)
matrix coln results1 = b ll95 ul95 
matrix rown results1 = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
matrix coln results2 = b ll95 ul95 
matrix rown results2 = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
matrix coln results3 = b ll95 ul95 
matrix rown results3 = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
	
foreach x in mgr2_v3   ///
			{
		
	eststo clear
	forval i =1/15{
		 
		 
		 if "`x'" == "ihs_stars" | "`x'" == "ihs_stars_m_r" | "`x'" == "ihs_stars_m_r_mgr" | "`x'" == "n_ihs_stars_m_r_mgr" ///
			| "`x'" == "ihs_stars_l" | "`x'" == "ihs_stars_u" ///
			| "`x'" == "ihs_stars_q1" | "`x'" == "ihs_stars_q2" | "`x'" == "ihs_stars_q3" | "`x'" == "ihs_stars_q4" ///
			| "`x'" == "ever_ihs_stars" ///
			| "`x'" == "ever_ihs_stars_q1" | "`x'" == "ever_ihs_stars_q2" | "`x'" == "ever_ihs_stars_q3" | "`x'" == "ever_ihs_stars_q4" ///
			| "`x'" == "ihs_stars_med" ///
			| "`x'" == "ihs_stars_q1_mgr" | "`x'" == "ihs_stars_q2_mgr" ///
			| "`x'" == "ihs_stars_q3_mgr" | "`x'" == "ihs_stars_q4_mgr" ///
			| "`x'" == "ihs_stars_q1_male_mgr2_v3" | "`x'" == "ihs_stars_q2_male_mgr2_v3" ///
			| "`x'" == "ihs_stars_q3_male_mgr2_v3" | "`x'" == "ihs_stars_q4_male_mgr2_v3" ///
			| "`x'" == "ihs_stars_q1_fem_mgr2_v3" | "`x'" == "ihs_stars_q2_fem_mgr2_v3" ///
			| "`x'" == "ihs_stars_q3_fem_mgr2_v3" | "`x'" == "ihs_stars_q4_fem_mgr2_v3" ///
			| "`x'" == "ihs_remote" | "`x'" == "ihs_employer" | "`x'" == "ihs_genderequal" ///
			| "`x'" == "ihs_familysupport" | "`x'" == "ihs_femaleleader" | "`x'" == "ihs_flexible" ///
			| "`x'" == "ihs_learning" | "`x'" == "ihs_mgmtopp" | "`x'" == "ihs_ML" | "`x'" == "ihs_paidtimeoff" ///
			| "`x'" == "ihs_salarysatisfy" | "`x'" == "ihs_belonging" | "`x'" == "ihs_social" ///
			| "`x'" == "ihs_sponsorship" | "`x'" == "ihs_diversity" | "`x'" == "ihs_people" ///
			| "`x'" == "ihs_wellness" | "`x'" == "ihs_dads_have_pl" | "`x'" == "ihs_received" ///
			| "`x'" == "ihs_growth" | "`x'" == "ihs_support" | "`x'" == "ihs_agree"{
			
		eststo `x'_`i': reg `x' femaleshare_male femaleshare_female i.female ///
			 i.Class##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
			 [aweight=ihs_ratings_count] ///
		 if year_since_grad==`i', cluster(section_id)
		 
		sum `x' [aweight=ihs_ratings_count] if year_since_grad==`i' & female==1, d
		local mean_f = r(mean)
		sum `x' [aweight=ihs_ratings_count] if year_since_grad==`i' & female==0, d
		local mean_m = r(mean)
		
		sum `x' [aweight=ihs_ratings_count] if year_since_grad==`i', d
		local mean = r(mean)
		local sd = r(sd)
		test femaleshare_male = femaleshare_female
		local p_value = r(p)
		eststo `x'_`i', addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
		local var 1.female
		cap matrix results1[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_female
		cap matrix results2[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_male
		cap matrix results3[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		}
		
		if "`x'" == "gd_i_totalpay"{
		eststo `x'_`i': reg `x' femaleshare_male femaleshare_female i.female ///
			 i.Class##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
			 [aweight=gd_i_reviews] ///
		 if year_since_grad==`i', cluster(section_id)
		 
		 sum `x' if year_since_grad==`i', d
		local mean = r(mean)
		local sd = r(sd)
		test femaleshare_male = femaleshare_female
		local p_value = r(p)
		eststo `x'_`i', addscalars(mean `mean' sd `sd' p_value `p_value')
		local var 1.female
		cap matrix results1[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_female
		cap matrix results2[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_male
		cap matrix results3[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']

		
		}
		 
		else{	
		 eststo `x'_`i': reg `x' femaleshare_male femaleshare_female i.female  ///
			 i.Class##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
			 if year_since_grad==`i', cluster(section_id)
		 
		sum `x' if year_since_grad==`i' & female==1, d
		local mean_f = r(mean)
		sum `x' if year_since_grad==`i' & female==0, d
		local mean_m = r(mean)
		
		sum `x' if year_since_grad==`i', d
		local mean = r(mean)
		local sd = r(sd)
		test femaleshare_male = femaleshare_female
		local p_value = r(p)
		eststo `x'_`i', addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
		local var 1.female
		cap matrix results1[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_female
		cap matrix results2[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_male
		cap matrix results3[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		}
		
	
	}
	
	
	coefplot (matrix(results2[,1]), ci((results2[,2] results2[,3])) label("Female share x Female")) ///
	(matrix(results3[,1]), ci((results3[,2] results3[,3])) label("Female share x Male") mcolor(black*.30) ciopts(color(black*.30)) ) ///
	 ,  ytitle("") yline(0) ///
	 base omitted ciopts(recast(rcap)) vertical ///
	 xtitle("Years since MBA graduation")  ///
	 plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) ///
	 legend(rows(1)) scheme(s2mono) 
	 graph export "${output}/graphs/Figure_6.eps", replace
	 
	 
}
}

* Figure 7: Effect of Female Peers on Female-Friendly Firms by Year Since Graduation
if `p7'==1{
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
	
local missings pre_any_mgr_v3 pre_mgr2_v3 pre_founder ///
		pre_i_fin pre_i_cons pre_i_good pre_i_hlth pre_i_tech pre_i_oth ///
		pre_small pre_medium pre_big pre_top_100_firm pre_size_LI ///
		ivy top20 pre_exp ///
		pre_mgr_v3 pre_vp_dir_v3 pre_svp_v3 pre_csuite_v3 ///
		pre_f_IT pre_f_acct pre_f_adm pre_f_cons pre_f_cus ///
		pre_f_fin pre_f_mgmt pre_f_HR pre_f_legal pre_f_mkt ///
		pre_f_opn pre_f_PR pre_f_prod pre_f_RD pre_f_sales pre_f_strat ///
		pre_PL pre_PL2 

		
foreach var of local missings {
		
		*create missing indicator 
		gen `var'_m = (`var'==.)
		
		*replace missing values with zero
		replace `var'=99 if `var'==.
    }
	


	
keep if year_since_grad>=1 & year_since_grad<=15


sort linkedin_id year_since_grad
foreach x in ihs_stars_med ///
	{
	gen temp = `x' if year_since_grad>=1
	by linkedin_id: gen ever_`x' = sum(temp)
	replace ever_`x' = 1 if ever_`x'>1 & ever_`x' !=.
	replace ever_`x' = . if year_since_grad < 1
	drop temp
}

*generate manager unconditional on working		
gen mgr2_v3_cond = mgr2_v3
replace mgr2_v3_cond = . if (employed==0 | employed==.) & mgr2_v3==0

gen i_male = (i_fin==1 | i_cons==1 | i_tech==1)
replace i_male =. if (i_good==. & i_hlth==. & i_fin==. & i_cons==. & i_tech==. & i_oth==.)

gen i_female = 1- i_male
replace i_female = 0 if i_oth==1

gen i_male_mgr2_v3 = i_male==1 & mgr2_v3==1
replace  i_male_mgr2_v3  = . if i_male==. | mgr2_v3==.
gen i_female_mgr2_v3 = i_female==1 & mgr2_v3==1
replace  i_female_mgr2_v3  = . if i_female==. | mgr2_v3==.

gen i_male_mgr2_v3_c  = i_male_mgr2_v3 
replace i_male_mgr2_v3_c = . if mgr2_v3 !=1

*industries 
gen i_categorical = 1 if i_fin==1
replace i_categorical = 2 if i_cons==1
replace i_categorical = 3 if i_good==1
replace i_categorical = 4 if i_hlth==1
replace i_categorical = 5 if i_tech==1
replace i_categorical = 6 if i_oth==1

lab def i_categorical 1 "Finance" 2 "Consulting" 3 "Cons. Good" 4 "Healthcare" 5 "Technology" 6 "Other"
lab value i_categorical i_categorical

*functions
encode function_broad, gen(f_broad_categorical)
ta function_broad, gen(function_broad)


rename gd_fem_share_senior_manager gd_fem_share_sr_mgr
rename gd_fem_pct_d_totalpay gd_fem_pct_totpay
rename gd_sr_mgr_fem_pct_d_totalpay gd_sr_mgr_fem_pct_totpay

gen comp_gap = - gd_fem_pct_d_basepay
gen sr_comp_gap = - gd_sr_mgr_fem_pct_d_basepay


*generate lee bounds on ihs_stars
gen ihs_stars_l = ihs_stars
replace ihs_stars_l = 1.1 if ihs_stars==.
gen ihs_stars_u = ihs_stars
replace ihs_stars_u = 4.9 if ihs_stars==.

gen ihs_stars_m = (ihs_stars==.)
gen gd_m = (gd_fem_share_emp==. & gd_fem_share_sr_mgr==. & gd_basepay==. & comp_gap==. & sr_comp_gap==.)
gen fgb_rating_m = (fgb_rating==.)

sum ihs_ratings_count
replace ihs_ratings_count = `r(min)' if ihs_ratings_count==. & ihs_stars==.

*************************************
* Manager and Family Friendly Firms *
*************************************
local fam_vars ihs_stars_med

foreach var in `fam_vars'{
	gen `var'_mgr = (`var'==1 & mgr2_v3==1)
	replace `var'_mgr=. if `var'==. | mgr2_v3==.
}	

gen ihs_stars_no_med_mgr = (ihs_stars_med==0 & mgr2_v3==1)
replace ihs_stars_no_med_mgr=. if ihs_stars_med==. | mgr2_v3==.


*create manager + external
preserve
drop if year_since_grad==1

collapse (max)external, by(linkedin_id firmnum mgr2_v3)
drop if firmnum==.
gen external_mgr = (external==1 & mgr2_v3==1)
replace external_mgr=. if external==. | mgr2_v3==.
gen internal_mgr = (mgr2_v3==1 & external_mgr==0)
replace internal_mgr=. if mgr2_v3==. | external_mgr==.
tempfile ext_int
save `ext_int', replace 
restore

merge m:1 linkedin_id firmnum mgr2_v3 using `ext_int'

*create total number of years in senior manager positions
bys linkedin_id: egen mgr2_v3_tot_years = total(mgr2_v3)

*rename variables with too long names
rename gd_sr_mgr_fem_pct_d_basepay gd_sr_mgr_fem_pct_d_base

gen gd_i_bonuspay = gd_i_totalpay - gd_i_basepay

gen log_gd_i_totalpay = ln(gd_i_totalpay+1)
gen log_gd_i_bonuspay = ln(gd_i_bonuspay+1)
gen log_gd_i_basepay = ln(gd_i_basepay+1)

*create manager + family friendly index + male dom
gen a_m_fam_fr_male_mgr2_v3 = (ihs_stars_med==1 & mgr2_v3==1 & i_male==1)
replace a_m_fam_fr_male_mgr2_v3=. if ihs_stars_med==. | mgr2_v3==. | i_male==.

gen b_m_fam_fr_male_mgr2_v3 = (ihs_stars_med==0 & mgr2_v3==1 & i_male==1)
replace b_m_fam_fr_male_mgr2_v3=. if ihs_stars_med==. | mgr2_v3==. | i_male==.

gen a_m_fam_fr_female_mgr2_v3 = (ihs_stars_med==1 & mgr2_v3==1 & i_female==1)
replace a_m_fam_fr_female_mgr2_v3=. if ihs_stars_med==. | mgr2_v3==. | i_female==.

gen b_m_fam_fr_female_mgr2_v3 = (ihs_stars_med==0 & mgr2_v3==1 & i_female==1)
replace b_m_fam_fr_female_mgr2_v3=. if ihs_stars_med==. | mgr2_v3==. | i_female==.
	
preserve
bys section_id:keep if _n==1
sum femaleshare,d
restore
local p25 = r(p25)
local p75 = r(p75)
local p1 = r(p1)
local p99 = r(p99)
local p10 = r(p10)
local p90 = r(p90)

eststo clear
matrix results1 = J(15,3,.)
matrix results2 = J(15,3,.)
matrix results3 = J(15,3,.)
matrix coln results1 = b ll95 ul95 
matrix rown results1 = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
matrix coln results2 = b ll95 ul95 
matrix rown results2 = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
matrix coln results3 = b ll95 ul95 
matrix rown results3 = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
	
foreach x in ihs_stars_med  ihs_stars_med_mgr ///
			{
		
	eststo clear
	forval i =1/15{
		 
		 if "`x'" == "ihs_stars" | "`x'" == "ihs_stars_m_r" | "`x'" == "ihs_stars_m_r_mgr" | "`x'" == "n_ihs_stars_m_r_mgr" ///
			| "`x'" == "ihs_stars_l" | "`x'" == "ihs_stars_u" ///
			| "`x'" == "ihs_stars_q1" | "`x'" == "ihs_stars_q2" | "`x'" == "ihs_stars_q3" | "`x'" == "ihs_stars_q4" ///
			| "`x'" == "ever_ihs_stars" ///
			| "`x'" == "ever_ihs_stars_q1" | "`x'" == "ever_ihs_stars_q2" | "`x'" == "ever_ihs_stars_q3" | "`x'" == "ever_ihs_stars_q4" ///
			| "`x'" == "ihs_stars_med" ///
			| "`x'" == "ihs_stars_q1_mgr" | "`x'" == "ihs_stars_q2_mgr" ///
			| "`x'" == "ihs_stars_q3_mgr" | "`x'" == "ihs_stars_q4_mgr" ///
			| "`x'" == "ihs_stars_q1_male_mgr2_v3" | "`x'" == "ihs_stars_q2_male_mgr2_v3" ///
			| "`x'" == "ihs_stars_q3_male_mgr2_v3" | "`x'" == "ihs_stars_q4_male_mgr2_v3" ///
			| "`x'" == "ihs_stars_q1_fem_mgr2_v3" | "`x'" == "ihs_stars_q2_fem_mgr2_v3" ///
			| "`x'" == "ihs_stars_q3_fem_mgr2_v3" | "`x'" == "ihs_stars_q4_fem_mgr2_v3" ///
			| "`x'" == "ihs_remote" | "`x'" == "ihs_employer" | "`x'" == "ihs_genderequal" ///
			| "`x'" == "ihs_familysupport" | "`x'" == "ihs_femaleleader" | "`x'" == "ihs_flexible" ///
			| "`x'" == "ihs_learning" | "`x'" == "ihs_mgmtopp" | "`x'" == "ihs_ML" | "`x'" == "ihs_paidtimeoff" ///
			| "`x'" == "ihs_salarysatisfy" | "`x'" == "ihs_belonging" | "`x'" == "ihs_social" ///
			| "`x'" == "ihs_sponsorship" | "`x'" == "ihs_diversity" | "`x'" == "ihs_people" ///
			| "`x'" == "ihs_wellness" | "`x'" == "ihs_dads_have_pl" | "`x'" == "ihs_received" ///
			| "`x'" == "ihs_growth" | "`x'" == "ihs_support" | "`x'" == "ihs_agree"{
			
		eststo `x'_`i': reg `x' femaleshare_male femaleshare_female i.female ///
			 i.Class##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
			 [aweight=ihs_ratings_count] ///
		 if year_since_grad==`i', cluster(section_id)
		 
		sum `x' [aweight=ihs_ratings_count] if year_since_grad==`i' & female==1, d
		local mean_f = r(mean)
		sum `x' [aweight=ihs_ratings_count] if year_since_grad==`i' & female==0, d
		local mean_m = r(mean)
		
		sum `x' [aweight=ihs_ratings_count] if year_since_grad==`i', d
		local mean = r(mean)
		local sd = r(sd)
		test femaleshare_male = femaleshare_female
		local p_value = r(p)
		eststo `x'_`i', addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
		local var 1.female
		cap matrix results1[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_female
		cap matrix results2[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_male
		cap matrix results3[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		}

		 
		else{	
		 eststo `x'_`i': reg `x' femaleshare_male femaleshare_female i.female  ///
			 i.Class##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
			 if year_since_grad==`i', cluster(section_id)
		 
		sum `x' if year_since_grad==`i' & female==1, d
		local mean_f = r(mean)
		sum `x' if year_since_grad==`i' & female==0, d
		local mean_m = r(mean)
		
		sum `x' if year_since_grad==`i', d
		local mean = r(mean)
		local sd = r(sd)
		test femaleshare_male = femaleshare_female
		local p_value = r(p)
		eststo `x'_`i', addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
		local var 1.female
		cap matrix results1[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_female
		cap matrix results2[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		local var c.femaleshare_male
		cap matrix results3[`i',1] = _b[`var'], _b[`var'] - 1.96*_se[`var'], _b[`var'] + 1.96*_se[`var']
		

		}
		
	
	}
	
		if "`x'" == "ihs_stars_med"{
			local l "a"
		}
		if "`x'" == "ihs_stars_med_mgr"{
			local l "b"
		}
	coefplot (matrix(results2[,1]), ci((results2[,2] results2[,3])) label("Female share x Female")) ///
	(matrix(results3[,1]), ci((results3[,2] results3[,3])) label("Female share x Male") mcolor(black*.30) ciopts(color(black*.30)) ) ///
	 ,  ytitle("") yline(0) ///
	 base omitted ciopts(recast(rcap)) vertical ///
	 xtitle("Years since MBA graduation")  ///
	 plotregion(fcolor(white)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) ///
	 legend(rows(1)) scheme(s2mono) 
	 graph export "${output}/graphs/Figure_7`l'.eps", replace	
	 
	 
}
}

* Figure 8: MBA Peer Support 
if `p8'==1{

use "${data}/raw/survey/MBA_survey.dta", clear
lab var femaleshare "Female share"
/*
-rank answers to questions: 
1. To what degree has your female MBA network helped your professional career by providing the following types of support (on a scale of 1 to 5, where 1 is not at all and 5 is to a great extent)?
2. To what degree has your male MBA network helped your professional career by providing the following types of support (on a scale of 1 to 5, where 1 is not at all and 5 is to a great extent)?*/


*recode the female_mba_impact answer values so 5 is the most and 1 is the least
foreach x of varlist female_mba_impact_* male_mba_impact_*{
	recode `x' (5=1) (4=2) (2=4) (1=5)
	label drop `x'
}

/*
Providing you with job referrals or other work-related opportunities (e.g., highlighting job openings, connecting with professionals who may be potential employers)
Providing you with general work advice, such as strategies for career advancement (e.g. negotiating an offer, finding a mentor)
Helping you identify female-friendly firms: i.e., firms that provide policies/benefits (e.g., maternity leave, flexible working schedule, mentoring programs for women) and/or have a work culture that support women's career advancement
Providing you with advice on how to balance work and family responsibilities (e.g., how to take advantage of maternity/parental leave)
Providing you with emotional support
Increasing your ambition and self-confidence
Improving your MBA academic experience (e.g., contributing to a supportive/intellectually stimulating learning environment)
Acting as your role models
Connecting you with mentors/sponsors*/

lab var female_mba_impact_1 "female MBA - job referrals"
lab var female_mba_impact_2 "female MBA - gen advice"
lab var female_mba_impact_3 "female MBA - find FF firms"
lab var female_mba_impact_4 "female MBA - work-family advice"
lab var female_mba_impact_5 "female MBA - emotional support"
lab var female_mba_impact_6 "female MBA - ambition"
lab var female_mba_impact_7 "female MBA - MBA academic experience"
lab var female_mba_impact_8 "female MBA - role model"
lab var female_mba_impact_9 "female MBA - find mentor"

lab var male_mba_impact_1 "male MBA - job referrals"
lab var male_mba_impact_2 "male MBA - gen advice"
lab var male_mba_impact_3 "male MBA - find FF firms"
lab var male_mba_impact_4 "male MBA - work-family advice"
lab var male_mba_impact_5 "male MBA - emotional support"
lab var male_mba_impact_6 "male MBA - ambition"
lab var male_mba_impact_7 "male MBA - MBA academic experience"
lab var male_mba_impact_8 "male MBA - role model"
lab var male_mba_impact_9 "male MBA - find mentor"

preserve
collapse female_mba_impact_* male_mba_impact_*
gen x = 1
reshape long female_mba_impact_ male_mba_impact_ ,i(x) j(type)
gen type_label = "Job Referrals" if type==1
replace type_label = "General Advice" if type==2
replace type_label = "Identify Female-Friendly Firms" if type==3
replace type_label =  "Work-Family Advice" if type==4
replace type_label = "Emotional Support" if type==5
replace type_label = "Increase Ambitions" if type==6
replace type_label = "MBA Experience" if type==7
replace type_label = "Role Model" if type==8
replace type_label = "Find Mentor" if type==9

rename female_mba_impact_ mba_impact1
rename male_mba_impact_ mba_impact0

reshape long mba_impact,i(type) j(female)

gen type_group = "Information" if type==1 | type==2 | type==3 | type==4 | type ==9
replace type_group = "Ambitions" if type==5 | type==6 | type==8
replace type_group = "MBA Exp." if type ==7


graph hbar (mean) mba_impact, over(female, relabel(1 "Male MBA" 2 "Female MBA"))  over(type_label, sort(1)  descending) over(type_group,label(angle(90)))   asyvars ytitle("Mean MBA Support" "(1=Not at all, 5=To a great extent)") nofill legend(position(6) row(1)) graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white)
graph export "${output}/graphs/Figure_8a.eps", replace


restore 

forval i = 1/9{
	gen female_mba_impact_dummy_`i' = female_mba_impact_`i'>3
	replace female_mba_impact_dummy_`i' = . if female_mba_impact_`i'==.
	lab var female_mba_impact_dummy_`i' "4 or 5 importance"
	
	gen male_mba_impact_dummy_`i' = male_mba_impact_`i'>3
	replace male_mba_impact_dummy_`i' = . if male_mba_impact_`i'==.
	lab var male_mba_impact_dummy_`i' "4 or 5 importance"

}


/*
-rank question above weighting them using questions: On a scale of 1 to 5, where 1 is not at all and 5 is extremely important, how would you rate the importance of the following types of support from your professional network for your career?

Multiply the importance value by the support value for each item for each respondent:
Weighted Support = Importance × Support

*/


preserve
keep responseid importance_support_* female_mba_impact_* male_mba_impact_* femaleshare Class
reshape long importance_support_ female_mba_impact_ male_mba_impact_ female_mba_impact_dummy_ male_mba_impact_dummy_, i(responseid femaleshare Class) j(support_type)

foreach x in female_mba_impact_ male_mba_impact_{
	gen wgt_`x' = `x'*importance_support_
}
bys responseid: egen total_support = sum(importance_support_)
foreach x in female_mba_impact_ male_mba_impact_{
	replace wgt_`x' = wgt_`x'/total_support
}

gen diff_wgt_female_male_mba_impact = wgt_female_mba_impact_ - wgt_male_mba_impact_

collapse diff_wgt_female_male_mba_impact,by(support_type)

gen type_label = "Job Referrals" if support_type==1
replace type_label = "General Advice" if support_type==2
replace type_label = "Identify Female-Friendly Firms" if support_type==3
replace type_label =  "Work-Family Advice" if support_type==4
replace type_label = "Emotional Support" if support_type==5
replace type_label = "Increase Ambitions" if support_type==6
replace type_label = "MBA Experience" if support_type==7
replace type_label = "Role Model" if support_type==8
replace type_label = "Find Mentor" if support_type==9


gen type_group = "Information" if support_type==1 | support_type==2 | support_type==3 | support_type==4 | support_type ==9
replace type_group = "Ambitions" if support_type==5 | support_type==6 | support_type==8
replace type_group = "MBA Exp." if support_type ==7

graph hbar (mean) diff_wgt_female_male_mba_impact, over(type_label,sort(1) descending ) over(type_group,label(angle(90))) ytitle("Mean Female-Male MBA Support" "(Weighted by Importance)") nofill graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) scheme(s2mono)
graph export "${output}/graphs/Figure_8b.eps",  replace
restore
}

* Figure 9: Effect of Female Peers on MBA Peer Support
if `p9'==1{
use "${data}/raw/survey/MBA_survey.dta", clear
lab var femaleshare "Female share"
/*
-rank answers to questions: 
1. To what degree has your female MBA network helped your professional career by providing the following types of support (on a scale of 1 to 5, where 1 is not at all and 5 is to a great extent)?
2. To what degree has your male MBA network helped your professional career by providing the following types of support (on a scale of 1 to 5, where 1 is not at all and 5 is to a great extent)?*/


*recode the female_mba_impact answer values so 5 is the most and 1 is the least
foreach x of varlist female_mba_impact_* male_mba_impact_*{
	recode `x' (5=1) (4=2) (2=4) (1=5)
	label drop `x'
}

/*
Providing you with job referrals or other work-related opportunities (e.g., highlighting job openings, connecting with professionals who may be potential employers)
Providing you with general work advice, such as strategies for career advancement (e.g. negotiating an offer, finding a mentor)
Helping you identify female-friendly firms: i.e., firms that provide policies/benefits (e.g., maternity leave, flexible working schedule, mentoring programs for women) and/or have a work culture that support women's career advancement
Providing you with advice on how to balance work and family responsibilities (e.g., how to take advantage of maternity/parental leave)
Providing you with emotional support
Increasing your ambition and self-confidence
Improving your MBA academic experience (e.g., contributing to a supportive/intellectually stimulating learning environment)
Acting as your role models
Connecting you with mentors/sponsors*/

lab var female_mba_impact_1 "female MBA - job referrals"
lab var female_mba_impact_2 "female MBA - gen advice"
lab var female_mba_impact_3 "female MBA - find FF firms"
lab var female_mba_impact_4 "female MBA - work-family advice"
lab var female_mba_impact_5 "female MBA - emotional support"
lab var female_mba_impact_6 "female MBA - ambition"
lab var female_mba_impact_7 "female MBA - MBA academic experience"
lab var female_mba_impact_8 "female MBA - role model"
lab var female_mba_impact_9 "female MBA - find mentor"

lab var male_mba_impact_1 "male MBA - job referrals"
lab var male_mba_impact_2 "male MBA - gen advice"
lab var male_mba_impact_3 "male MBA - find FF firms"
lab var male_mba_impact_4 "male MBA - work-family advice"
lab var male_mba_impact_5 "male MBA - emotional support"
lab var male_mba_impact_6 "male MBA - ambition"
lab var male_mba_impact_7 "male MBA - MBA academic experience"
lab var male_mba_impact_8 "male MBA - role model"
lab var male_mba_impact_9 "male MBA - find mentor"


forval i = 1/9{
	gen female_mba_impact_dummy_`i' = female_mba_impact_`i'>3
	replace female_mba_impact_dummy_`i' = . if female_mba_impact_`i'==.
	lab var female_mba_impact_dummy_`i' "4 or 5 importance"
	
	gen male_mba_impact_dummy_`i' = male_mba_impact_`i'>3
	replace male_mba_impact_dummy_`i' = . if male_mba_impact_`i'==.
	lab var male_mba_impact_dummy_`i' "4 or 5 importance"

}
 


/*
-effect of female peers on previous questions
*/
gen femaleshare_abovemed = (femaleshare> 0.34)
replace femaleshare_abovemed=. if femaleshare==.



// female mba support continuous 
matrix results = J(9,3,.)
matrix coln results = b ll95 ul95 
forval i = 1/9{
	areg female_mba_impact_`i' femaleshare, ab(Class)
	matrix results[`i',1] = _b[femaleshare], _b[femaleshare] -1.96*_se[femaleshare], _b[femaleshare] +1.96*_se[femaleshare]
}

	
	
coefplot ///
	(matrix(results[,1]), ci((results[,2] results[,3])) rename(r1="Job Referrals" r2 = "General Advice" r3 = "Identify Female-Friendly Firms" r4 =  "Work-Family Advice" r5 = "Emotional Support" r6 = "Increase Ambitions" r7 = "MBA Experience" r8 = "Role Model" r9 = "Find Mentor") offset(0) nokey ///
	 mlabsize(medium)) , ///
	 ytitle("") xline(0) ///
	 base omitted  ///
	 graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) legend(rows(1)) scheme(s2mono)
	 graph export "${output}/graphs/Figure_9a.eps",  replace
	
// male mba support continuous  
matrix results = J(9,3,.)
matrix coln results = b ll95 ul95 
forval i = 1/9{
	areg male_mba_impact_`i' femaleshare, ab(Class)
	matrix results[`i',1] = _b[femaleshare], _b[femaleshare] -1.96*_se[femaleshare], _b[femaleshare] +1.96*_se[femaleshare]
}

	
coefplot ///
	(matrix(results[,1]), ci((results[,2] results[,3])) rename(r1="Job Referrals" r2 = "General Advice" r3 = "Identify Female-Friendly Firms" r4 =  "Work-Family Advice" r5 = "Emotional Support" r6 = "Increase Ambitions" r7 = "MBA Experience" r8 = "Role Model" r9 = "Find Mentor") offset(0) nokey ///
	 mlabsize(medium)) , ///
	 ytitle("") xline(0) ///
	 base omitted  ///
	 graphregion(fcolor(white) ifcolor(white) color(white) icolor(white)) bgcolor(white) legend(rows(1)) scheme(s2mono)
	 graph export "${output}/graphs/Figure_9b.eps",  replace
}



* Table 1: Match Statistics
if `p10'==1{
	
use "${data}/raw/alumni_names_gender_fake.dta",clear
keep if Class >=2000 & Class <=2010
drop if Class == 2009
gen alumni = 1 
collapse (sum) alumni female,by(Class SectionGroupNumber)
gen male = alumni - female
rename female alumni_female
rename male alumni_male
tostring SectionGroupNumber, replace
tempfile alumni
save `alumni'
	
use "${data}/raw/LI_workhistory_yearly_educ_gender_fake.dta",clear
keep if Class <=2018
keep if year_since_grad >=1 & year_since_grad<=15
bys linkedin_id: keep if _n==1
gen matched = 1 
gen matched_female = 1 if female==1
collapse (sum) matched matched_female, by(SectionGroupNumber Class)
gen matched_male = matched-matched_female
tostring SectionGroupNumber,replace
merge 1:1 SectionGroupNumber Class using "${data}/raw/admin_section_data_allyears_fake.dta"
drop if Class==2009 
keep if Class >= 2000 & Class <=2018
keep if _merge ==3 | _merge ==2
 
drop _merge
merge 1:1 SectionGroupNumber Class using `alumni'
keep if _merge==3 | _merge==1 
drop _merge

count
forval i = 1/`r(N)'{
	levelsof SectionGroupNumber if _n==`i',local(section)
	levelsof Class if _n==`i',local(class)
	levelsof females_section if  _n==`i' , local(females)
	levelsof males_section if _n==`i' , local(males)
	levelsof total_section if _n==`i', local(total)
	levelsof matched_female if _n==`i',local(matched_female)
	levelsof matched_male if _n==`i',local(matched_male)
	levelsof alumni if  _n==`i', local(total_alumni)
	levelsof alumni_female if  _n==`i', local(alumni_female)
	levelsof alumni_male if  _n==`i', local(alumni_male)

	preserve
	clear 
	set obs `total'
	
	gen Class = `class'
	gen SectionGroupNumber = `section'
	gen females_section = `females'
	gen males_section = `males'
	gen total_section = `total'
	gen matched_female = `matched_female'
	gen matched_male = `matched_male'
	if `class' <=2010{
		gen alumni = `total_alumni'
	}
	
	gen female = 0
	replace female = 1 if _n <=`females'
	
	gsort -female
	gen matched = 0 
	replace matched = 1 if _n<=`matched_female'
	gen matched_alumni = 0 
	if `class' <=2010{
		replace matched_alumni = 1 if _n <= `alumni_female' & female==1 
	}
	sort female
	replace matched = 1 if _n<=`matched_male'
	if `class' <=2010{
		replace matched_alumni = 1 if _n <= `alumni_male' & female==0
	}
	

	
	
	if `i'==1{
		tempfile temp
		save `temp'
	}
	else{
		append using `temp'
		save `temp',replace
	}
	restore
}

cap file close myfile
file open myfile using "${output}/tables/Table_1.tex", write replace
file write myfile "\begin{tabular}{lccccccc}" _n ///
	"\toprule" _n ///
	"Data Source & \multicolumn{3}{c}{Match Rate} & \multicolumn{2}{c}{$Female Share \times Male$} & \multicolumn{2}{c}{$Female Share \times Female$}  \\" _n ///
	" & All & Male  &  Female & \shortstack{No \\ Controls} & \shortstack{With \\ Class(-Year) \\ FE} & \shortstack{No \\ Controls} & \shortstack{With \\ Class(-Year) \\ FE}  \\" _n ///
	"\midrule" _n /// 
"\multicolumn{8}{c}{\textbf{A. Individuals (Person-Level) -- Cohorts 2000-2008, 2010-2018}} \\" _n
	




use `temp',clear
gen femaleshare = (females_section - female)/(total_section-1)
gen femaleshare_male = femaleshare * (1-female)
gen femaleshare_female = femaleshare * female

egen section_id = group(Class SectionGroupNumber)

foreach y in matched_alumni matched{

	if "`y'" == "matched_alumni"{
		local label "With Alumni Profiles - 2000-2010 Only"
		sum `y' if Class <=2010 
	}
	else{
		local label "With LinkedIn Profiles"
		sum `y'
	}
	scalar matchrate = r(mean)
	local matchrate: di %5.3f scalar(matchrate)
	
	if "`y'" == "matched_alumni"{
		sum `y' if Class <=2010 & female == 0
	}
	else{
		sum `y' if female==0
	}
	scalar matchrate_m = r(mean)
	local matchrate_m: di %5.3f scalar(matchrate_m)

	if "`y'" == "matched_alumni"{
		sum `y' if Class <=2010 & female == 1
	}
	else{
		sum `y' if female==1
	}
	scalar matchrate_f = r(mean)
	local matchrate_f: di %5.3f scalar(matchrate_f)
	
	forval k = 1/2{
		if `k'==1{
			local controls 
		}
		else{
			local controls female##i.Class
		}
		
		if "`y'" == "matched_alumni"{
			reg `y' femaleshare_male femaleshare_female female `controls' if Class <=2010 , cluster(Class)
		}
		else{
			reg `y' femaleshare_male femaleshare_female female `controls', cluster(Class)
		}
		scalar beta_m`k' = _b[femaleshare_male]
		local beta_m`k': di %9.3f scalar(beta_m`k')
		
		scalar beta_f`k' = _b[femaleshare_female]
		local beta_f`k': di %9.3f scalar(beta_f`k')
		
		local beta_m_p =2*ttail(e(df_r),abs(_b[femaleshare_male]/_se[femaleshare_male]))
		local beta_f_p =2*ttail(e(df_r),abs(_b[femaleshare_female]/_se[femaleshare_female]))
		foreach g in m f{
			local stars_`g'`k' = ""
			if `beta_`g'_p'<=.1{
				local stars_`g'`k' = "*"
			}
			if `beta_`g'_p'<=.05{
				local stars_`g'`k' = "**"
			}
			if `beta_`g'_p'<=.01{
				local stars_`g'`k' = "***"
			}
		}
		
		scalar se_m`k' = _se[femaleshare_male]
		local se_m`k': di %4.3f scalar(se_m`k')
		scalar se_f`k' = _se[femaleshare_female]
		local se_f`k': di %4.3f scalar(se_f`k')
	}
	

	file write myfile "`label'  & `matchrate' & `matchrate_m' & `matchrate_f' & `beta_m1'`stars_m1' & `beta_m2'`stars_m2' & `beta_f1'`stars_f1' & `beta_f2'`stars_f2' \\" _n
	file write myfile " &  &  & & (`se_m1') & (`se_m2') & (`se_f1') & (`se_f2') \\" _n
	
	file write myfile " & & & & & & & \\" _n
	
}
	
	
file write myfile "\multicolumn{8}{c}{\textbf{B. Firms (Person-Year Level) -- Cohorts 2000-2008, 2010-2018}} \\" _n


use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
keep if Class <=2018
keep if year_since_grad >=1 & year_since_grad<=15



gen has_linkedin_company = firm_id_LI !=""
gen has_glassdoor = gd_reviews !=. 
gen has_ihs = firm_id_ihs !=. 
gen has_fgb = paid_weeks !=. 
gen has_wob = any_board_woman  !=. 

foreach y in has_linkedin_company has_glassdoor has_ihs has_fgb has_wob{

	if "`y'" == "has_linkedin_company"{
		local label "With LinkedIn Company Profiles"
	}
	else if "`y'" == "has_glassdoor"{
		local label "With Glassdoor"
		
	}
	else if "`y'" == "has_ihs"{
		local label "With InHerSight"
		
	}
	else if "`y'" == "has_fgb"{
		local label "With FairyGodBoss"
		
	}
	else if "`y'" == "has_wob"{
		local label "With Women On Boards"
		
	}
	
	sum `y'
	scalar matchrate = r(mean)
	local matchrate: di %5.3f scalar(matchrate)
	
	
	sum `y' if female==0
	scalar matchrate_m = r(mean)
	local matchrate_m: di %5.3f scalar(matchrate_m)

	
	sum `y' if female==1
	scalar matchrate_f = r(mean)
	local matchrate_f: di %5.3f scalar(matchrate_f)
	
	forval k = 1/2{
		if `k'==1{
			local controls 
		}
		else{
			local controls female##i.Class##i.year
		}
		
		
		reg `y' femaleshare_male femaleshare_female female `controls', cluster(Class)

		scalar beta_m`k' = _b[femaleshare_male]
		local beta_m`k': di %9.3f scalar(beta_m`k')
		
		scalar beta_f`k' = _b[femaleshare_female]
		local beta_f`k': di %9.3f scalar(beta_f`k')
		
		local beta_m_p =2*ttail(e(df_r),abs(_b[femaleshare_male]/_se[femaleshare_male]))
		local beta_f_p =2*ttail(e(df_r),abs(_b[femaleshare_female]/_se[femaleshare_female]))
		foreach g in m f{
			local stars_`g'`k' = ""
			if `beta_`g'_p'<=.1{
				local stars_`g'`k' = "*"
			}
			if `beta_`g'_p'<=.05{
				local stars_`g'`k' = "**"
			}
			if `beta_`g'_p'<=.01{
				local stars_`g'`k' = "***"
			}
		}
		
		scalar se_m`k' = _se[femaleshare_male]
		local se_m`k': di %4.3f scalar(se_m`k')
		scalar se_f`k' = _se[femaleshare_female]
		local se_f`k': di %4.3f scalar(se_f`k')
	}
	

	file write myfile "`label'  & `matchrate' & `matchrate_m' & `matchrate_f' & `beta_m1'`stars_m1' & `beta_m2'`stars_m2' & `beta_f1'`stars_f1' & `beta_f2'`stars_f2' \\" _n
	file write myfile " &  &  & & (`se_m1') & (`se_m2') & (`se_f1') & (`se_f2') \\" _n
	
	file write myfile " & & & & & & & \\" _n
	
}

file write myfile "\bottomrule" _n "\end{tabular}" 
file close myfile


}

* Table 2: Randomization Test - Guryan et al., 2009 and Caeyers and Fafchamps, 2024
if `p11'==1{
	
*Table 2a
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
keep if year_since_grad>=1 & year_since_grad<=15
sort linkedin_id, stable
by linkedin_id: keep if _n==1

bys Class: egen female_n_class = total(female)
replace female_n_class = female_n_class -1 if female==1
bys Class: gen number_class = _N
bys Class: gen femaleshare_class = female_n_class/(number_class-1)

gen pre_female = female 

foreach x in female pre_any_mgr_v3 pre_mgr2_v3 pre_founder ///
		pre_i_fin pre_i_cons pre_i_good pre_i_hlth pre_i_tech pre_i_oth ///
		pre_small pre_medium pre_big pre_top_100_firm ivy top20{
	bys section_id: egen sum_`x' = total(`x') 
	bys section_id: egen ct_`x' = count(`x')
	replace sum_`x' = sum_`x' - `x'
	gen s_`x' = sum_`x'/(ct_`x'-1)
	drop sum_`x' ct_`x'
	
	bys Class: egen sum_`x' = total(`x') 
	bys Class: egen ct_`x' = count(`x')
	replace sum_`x' = sum_`x' - `x'
	gen c_`x' = sum_`x'/(ct_`x'-1)
	drop sum_`x' ct_`x'
}


lab var femaleshare_class "Class female share"



*test for random assignment
eststo clear

*Matt and Jon' test
foreach var in female {
	eststo class_c_`var': reg `var' femaleshare femaleshare_class i.Class ///
			,cluster(section_id)
	estadd local classfe "Yes" , replace
	
	
	eststo class_c_`var'_2: reg `var' femaleshare femaleshare_class top20 pre_mgr2_v3 pre_i_fin i.Class ///
			,cluster(section_id)
	estadd local classfe "Yes" , replace
	
	eststo class_c_`var'_2011: reg `var' femaleshare femaleshare_class i.Class ///
			 if Class>=2011, cluster(section_id)
	estadd local classfe "Yes" , replace
	
	eststo class_c_`var'_2011_2: reg `var' femaleshare femaleshare_class top20 pre_mgr2_v3 pre_i_fin i.Class ///
			 if Class>=2011, cluster(section_id)
	estadd local classfe "Yes" , replace
}

label var  femaleshare "Average(X), Section Peers" 
label var  femaleshare_class "Average(X), Class Peers" 


esttab class_c_female class_c_female_2 class_c_female_2011 class_c_female_2011_2 ///
using "${output}/tables/Table_2a.tex", replace ///
 keep(femaleshare femaleshare_class) ///
 stats(r2 N classfe,fmt( %9.0g) labels("\(R^2\)" "N" "Class FE")) ///
 star(* .10 ** .05 *** .01)  se  /// 
 label interaction(" $\times$ ") mtitles("No Controls" "With Controls" "No Controls" "With Controls") ///
	 mgroups("2000-2018" "2011-2018", pattern(1 0 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span})) ///
 order(femaleshare femaleshare_class) booktabs f noomitted nobaselevel

 
*Table 2b
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
keep if year_since_grad>=1 & year_since_grad<=15

sort linkedin_id, stable
by linkedin_id: keep if _n==1

bys Class: gen n_class = _N
bys section_id: gen n_section = _N

keep Class SectionGroupNumber n_class n_section

sort Class SectionGroupNumber, stable
by Class SectionGroupNumber: keep if _n==1

gen L_k = n_class
gen K_k = n_section
egen M = total(K_k)


*Proposition 2 equation 5 
gen plim = - (L_k -1)*(K_k-1)/((L_k-K_k)*L_k + (K_k-1))
gen s2_zk = ((K_k - 1) + (L_k - K_k)*L_k)/(L_k*(L_k-1)*(K_k-1))
gen s2_zt = K_k/M*s2_zk 
egen s2_z = total(s2_zt)

gen rhot = K_k/M*(s2_zk/s2_z)*plim
egen rho = total(rhot)

keep if _n==1
levelsof rho , local(rho)


use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
keep if year_since_grad>=1 & year_since_grad<=15

gen rho = `rho'



sort linkedin_id, stable
by linkedin_id: keep if _n==1

gen female_tilde = female - rho*femaleshare
eststo nocontrols: reg female_tilde femaleshare  i.Class,cluster(Class)
estadd local classfe "Yes" , replace
estadd local controlsfe "No" , replace


eststo nocontrols2: reg female_tilde femaleshare  i.Class if Class >=2011 & Class <=2018,cluster(Class)
estadd local classfe "Yes" , replace
estadd local controlsfe "No" , replace


* add version with controls (need to partial out first)
* deviation from class averages for all variables 
foreach x in female femaleshare top20 pre_mgr2 pre_i_fin{
	bys Class: egen class_mean_`x' = mean(`x') 
	
	gen `x'_d = `x' - class_mean_`x'
}

reg female_d top20_d pre_mgr2_d pre_i_fin_d
predict female_resid, resid 

reg femaleshare_d top20_d pre_mgr2_d pre_i_fin_d
predict femaleshare_resid, resid 


gen female_resid_tilde = female_resid - rho*femaleshare_resid

eststo controls: reg female_resid_tilde femaleshare_resid  i.Class,cluster(Class)
estadd local classfe "Yes" , replace
estadd local controlsfe "Yes" , replace

eststo controls2: reg female_resid_tilde femaleshare_resid  i.Class if Class >=2011 & Class <=2018,cluster(Class)
estadd local classfe "Yes" , replace
estadd local controlsfe "No" , replace


esttab nocontrols controls nocontrols2 controls2  ///
	using "${output}/tables/Table_2b.tex", replace ///
	rename(femaleshare_resid femaleshare) ///
	 keep(femaleshare ) se star(* .10 ** .05 *** .01) r2 ///
	 s(r2 N classfe   , ///
	 label("\(R^2\)" "N" "Class FE" )) ///
	 label interaction(" $\times$ ") mtitles("No Controls" "With Controls" "No Controls" "With Controls") ///
	 mgroups("2000-2018" "2011-2018", pattern(1 0 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span})) ///
	 booktabs f noomitted nobaselevel

}

* Table 3: Effect of Female Peers on Senior Management
if `p12'==1{
	
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
	
local missings pre_any_mgr_v3 pre_mgr2_v3 pre_founder ///
		pre_i_fin pre_i_cons pre_i_good pre_i_hlth pre_i_tech pre_i_oth ///
		pre_small pre_medium pre_big pre_top_100_firm pre_size_LI ///
		ivy top20 pre_exp ///
		pre_mgr_v3 pre_vp_dir_v3 pre_svp_v3 pre_csuite_v3 ///
		pre_f_IT pre_f_acct pre_f_adm pre_f_cons pre_f_cus ///
		pre_f_fin pre_f_mgmt pre_f_HR pre_f_legal pre_f_mkt ///
		pre_f_opn pre_f_PR pre_f_prod pre_f_RD pre_f_sales pre_f_strat ///
		pre_PL2 

		
foreach var of local missings {
		
		*create missing indicator 
		gen `var'_m = (`var'==.)
		
		*replace missing values with zero
		replace `var'=99 if `var'==.
    }
	

	
keep if year_since_grad>=1 & year_since_grad<=15


sort linkedin_id year_since_grad



foreach x in mgr2_v3 {
		
		eststo `x'_noc: reg `x' femaleshare_male femaleshare_female i.female ///
		i.Class##i.year_since_grad##i.female ///
		, cluster(section_id)
			
			sum `x' if female==1, d
			local mean_f = r(mean)
			sum `x' if female==0, d
			local mean_m = r(mean)
		
			sum `x', d
			local mean = r(mean)
			local sd = r(sd)
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			eststo `x'_noc, addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
			estadd local classfe "Yes" , replace
			estadd local strat_c "No" , replace
			estadd local pred_c "No" , replace
			estadd local section_c "No" , replace
			
		
		
		eststo `x'_c: reg `x' femaleshare_male femaleshare_female i.female ///
		 i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
		 , cluster(section_id)
			
			sum `x' if female==1, d
			local mean_f = r(mean)
			sum `x' if female==0, d
			local mean_m = r(mean)
			
			sum `x', d
			local mean = r(mean)
			local sd = r(sd)
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			eststo `x'_c, addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
			estadd local classfe "Yes" , replace
			estadd local strat_c "Yes" , replace
			estadd local pred_c "No" , replace
			estadd local section_c "No" , replace

			
			
		eststo `x'_cc: reg `x' femaleshare_male femaleshare_female i.female ///
		 i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
		 , cluster(section_id)
			
			sum `x' if female==1, d
			local mean_f = r(mean)
			sum `x' if female==0, d
			local mean_m = r(mean)
			
			sum `x', d
			local mean = r(mean)
			local sd = r(sd)
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			eststo `x'_cc, addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
			estadd local classfe "Yes" , replace
			estadd local strat_c "Yes" , replace
			estadd local pred_c "Yes" , replace
			estadd local section_c "No" , replace
			
			
			
		eststo `x'_ccc: reg `x' femaleshare_male femaleshare_female i.female ///
		 i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
		 , cluster(section_id)
		 
			sum `x' if female==1, d
			local mean_f = r(mean)
			sum `x' if female==0, d
			local mean_m = r(mean)
			
			sum `x', d
			local mean = r(mean)
			local sd = r(sd)
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			eststo `x'_ccc, addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
			estadd local classfe "Yes" , replace
			estadd local strat_c "Yes" , replace
			estadd local pred_c "Yes" , replace
			estadd local section_c "Yes" , replace
	

}
	 
	esttab  mgr2_v3_noc mgr2_v3_c mgr2_v3_cc mgr2_v3_ccc using "${output}/tables/Table_3.tex", replace ///
	 keep(femaleshare_male femaleshare_female) se star(* .10 ** .05 *** .01) r2 ///
	 s(p_value mean_f mean_m r2 N classfe strat_c pred_c section_c, ///
	 label("\(p\)-value Male vs. Female" "Female Mean" "Male Mean" "\(R^2\)" "N" ///
	 "Class x Year x Female FE" "Stratification Controls" ///
	 "Pre-MBA Characteristics Controls" "Section-level Controls") ///
	 fmt(%9.3f %9.3f %9.3f %9.3f %9.3g)) ///
	 label interaction(" $\times$ ") mtitles("\shortstack{Senior-Level \\ Manager}" ///
	 "\shortstack{Senior-Level \\ Manager}" "\shortstack{Senior-Level \\ Manager}" ///
	 "\shortstack{Senior-Level \\ Manager}") booktabs f noomitted nobaselevel	
	
}
	
* Table 4: Effect of Female Peers on Compensation
if `p13'==1{
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
	
local missings pre_any_mgr_v3 pre_mgr2_v3 pre_founder ///
		pre_i_fin pre_i_cons pre_i_good pre_i_hlth pre_i_tech pre_i_oth ///
		pre_small pre_medium pre_big pre_top_100_firm pre_size_LI ///
		ivy top20 pre_exp ///
		pre_mgr_v3 pre_vp_dir_v3 pre_svp_v3 pre_csuite_v3 ///
		pre_f_IT pre_f_acct pre_f_adm pre_f_cons pre_f_cus ///
		pre_f_fin pre_f_mgmt pre_f_HR pre_f_legal pre_f_mkt ///
		pre_f_opn pre_f_PR pre_f_prod pre_f_RD pre_f_sales pre_f_strat ///
		pre_PL pre_PL2 

		
foreach var of local missings {
		
		*create missing indicator 
		gen `var'_m = (`var'==.)
		
		*replace missing values with zero
		replace `var'=99 if `var'==.
    }
	

	
keep if year_since_grad>=1 & year_since_grad<=15


sort linkedin_id year_since_grad



rename gd_fem_share_senior_manager gd_fem_share_sr_mgr
rename gd_fem_pct_d_totalpay gd_fem_pct_totpay
rename gd_sr_mgr_fem_pct_d_totalpay gd_sr_mgr_fem_pct_totpay

gen comp_gap = - gd_fem_pct_d_basepay
gen sr_comp_gap = - gd_sr_mgr_fem_pct_d_basepay


*rename variables with too long names
rename gd_sr_mgr_fem_pct_d_basepay gd_sr_mgr_fem_pct_d_base

gen gd_i_bonuspay = gd_i_totalpay - gd_i_basepay

gen log_gd_i_totalpay = ln(gd_i_totalpay+1)
gen log_gd_i_bonuspay = ln(gd_i_bonuspay+1)
gen log_gd_i_basepay = ln(gd_i_basepay+1)


replace gd_totalpay = gd_totalpay/1000
replace gd_totalpaysenior_manager = gd_totalpaysenior_manager/1000

*POOLED REGRESSIONS
foreach x in log_gd_i_totalpay log_gd_i_bonuspay log_gd_i_basepay gd_totalpay gd_totalpaysenior_manager ///
			{
		

	
		if  "`x'" == "log_gd_i_totalpay" | "`x'" == "log_gd_i_bonuspay" ///
			| "`x'" == "log_gd_i_basepay" {
			
		eststo `x'_w: reg `x' femaleshare_male femaleshare_female i.female ///
		 i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
             c.m_pre_mgr2_v3 ///
             c.m_pre_i_fin ///
             c.m_pre_i_cons ///
             c.m_pre_i_oth ///
             c.m_pre_PL2 ///
             c.m_white ///
             c.m_foreign ///
		 [aweight=gd_i_reviews], cluster(section_id)
			
			sum `x' [aweight=gd_i_reviews] if female==1, d
			local mean_f = r(mean)
			sum `x' [aweight=gd_i_reviews] if female==0, d
			local mean_m = r(mean) 
			
			sum `x' [aweight=gd_i_reviews], d
			local mean = r(mean)
			local sd = r(sd)
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			eststo `x'_w, addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
			estadd local classfe "Yes" , replace
			estadd local ind_fe "Yes" , replace

		
		}
		
		
		if "`x'" == "gd_totalpay" | "`x'" == "gd_totalpaysenior_manager" {
			
		eststo `x'_w: reg `x' femaleshare_male femaleshare_female i.female ///
		 i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
		 [aweight=gd_reviews], cluster(section_id)
			
			sum `x' [aweight=gd_reviews] if female==1, d
			local mean_f = r(mean)
			sum `x' [aweight=gd_reviews] if female==0, d
			local mean_m = r(mean)
			
			sum `x' [aweight=gd_reviews], d
			local mean = r(mean)
			local sd = r(sd)
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			eststo `x'_w, addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' p_value `p_value')
			estadd local classfe "Yes" , replace
			estadd local ind_fe "Yes" , replace
		}
			
}

use "${data}/raw/survey/MBA_survey.dta", clear

* Recode compensation
gen annual_income = .
replace annual_income = 125000 if compensation ==6
replace annual_income = 175000 if compensation ==7
replace annual_income = 250000 if compensation ==8
replace annual_income = 350000 if compensation ==9
replace annual_income = 450000 if compensation ==10
replace annual_income = 625000 if compensation ==11
replace annual_income = 875000 if compensation ==12
replace annual_income = 1000000 if compensation ==13
replace annual_income = . if compensation==3

gen log_annual_income = log(annual_income)
	
*regressions
foreach y in log_annual_income {
					
	eststo `y': areg `y' femaleshare,ab(Class) 
	estadd local ind_fe "Yes" , replace
	estadd local classfe "No" , replace

	sum `y'
	local mean_f = r(mean)
	local sd = r(sd)
	eststo `y', addscalars(mean_f `mean_f' sd `sd' )
}
				
				
	 
esttab log_gd_i_totalpay_w ///
	log_gd_i_basepay_w ///
	log_gd_i_bonuspay_w ///
	log_annual_income gd_totalpay_w gd_totalpaysenior_manager_w ///
	using "${output}/tables/Table_4.tex", replace ///
	rename(femaleshare femaleshare_female) coeflabels(femaleshare_male "Female share \(\times\) Male" femaleshare_female "Female share \(\times\) Female" ) ///
	 keep(femaleshare_male femaleshare_female ) se star(* .10 ** .05 *** .01) r2 ///
	 s(p_value mean_f mean_m r2 N ind_fe classfe, ///
	 label("\(p\)-value Male vs. Female" "Female Mean" "Male Mean" "\(R^2\)" "N" ///
	 "Class x Female FE" ///
	 "Class x Year x Female FE") ///
	 fmt(%9.3f %9.3f %9.3f %9.3g)) ///
	 label interaction(" $\times$ ") mtitles("\shortstack{Log Total \\ Annual \\ Compensation \\ (Imp.)}" ///
	 "\shortstack{Log Base \\ Annual \\ Compensation \\ (Imp.)}" ///
	 "\shortstack{Log Non-Base \\  Annual \\ Compensation \\ (Imp.)}" "\shortstack{Log Total \\ Annual \\ Compensation \\ (Survey)}" "\shortstack{Average Firm \\ Total Annual \\ Compensation \\ ('000s)}" ///
	  "\shortstack{Average Firm \\ Senior Manager \\ Total Annual \\ Compensation \\ ('000s)}") booktabs f noomitted nobaselevel 
}	
	
* Table 5: Effect of Female Peers on Probability of Senior Management in Female-Friendly Firms
if `p14'==1{
	
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
	
local missings pre_any_mgr_v3 pre_mgr2_v3 pre_founder ///
		pre_i_fin pre_i_cons pre_i_good pre_i_hlth pre_i_tech pre_i_oth ///
		pre_small pre_medium pre_big pre_top_100_firm pre_size_LI ///
		ivy top20 pre_exp ///
		pre_mgr_v3 pre_vp_dir_v3 pre_svp_v3 pre_csuite_v3 ///
		pre_f_IT pre_f_acct pre_f_adm pre_f_cons pre_f_cus ///
		pre_f_fin pre_f_mgmt pre_f_HR pre_f_legal pre_f_mkt ///
		pre_f_opn pre_f_PR pre_f_prod pre_f_RD pre_f_sales pre_f_strat ///
		pre_PL pre_PL2 
		
foreach var of local missings {
		
		*create missing indicator 
		gen `var'_m = (`var'==.)
		
		*replace missing values with zero
		replace `var'=99 if `var'==.
    }
	
	
keep if year_since_grad>=1 & year_since_grad<=15


sort linkedin_id year_since_grad



*************************************
* Manager and Family Friendly Firms *
*************************************
local fam_vars ihs_stars_med

foreach var in `fam_vars'{
	gen `var'_mgr = (`var'==1 & mgr2_v3==1)
	replace `var'_mgr=. if `var'==. | mgr2_v3==.
}	

gen ihs_stars_no_med_mgr = (ihs_stars_med==0 & mgr2_v3==1)
replace ihs_stars_no_med_mgr=. if ihs_stars_med==. | mgr2_v3==.


	
*PRESENTATION TABLE FEMALE FRIENDLY AND SENIOR MANAGER 
foreach x in ihs_stars_med ihs_stars_med_mgr ihs_stars_no_med_mgr mgr2_v3 ///
			{
		
		eststo `x': reg `x' femaleshare_male femaleshare_female i.female ///
		 i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
		 [aweight=ihs_ratings_count] ///
		 , cluster(section_id)
		 
			sum `x' [aweight=ihs_ratings_count]  if female==1 , d
            local mean_f = r(mean)
            sum `x' [aweight=ihs_ratings_count]  if female==0, d
            local mean_m = r(mean)
			
			sum `x' [aweight=ihs_ratings_count] , d
			local mean = r(mean)
			local sd = r(sd)
			
			sum femaleshare_female, d
			local p25 = r(p25)
			local p75 = r(p75)
			
			lincom femaleshare_female*`p75' - femaleshare_female*`p25'
			local estimate25_75 = r(estimate)
			local p_value_est25_75 = 2*ttail(r(df),abs(r(estimate)/r(se)))
			
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			eststo `x', addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' ///
										p_value `p_value' estimate25_75 `estimate25_75' ///
										p_value_est25_75 `p_value_est25_75')
			estadd local classfe "Yes" , replace
			estadd local ind_fe "Yes" , replace

		
			
		if "`x'"=="mgr2_v3"{
			
			
		eststo `x'_med: reg `x' femaleshare_male femaleshare_female i.female ///
		 i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
		 [aweight=ihs_ratings_count] ///
		 if ihs_stars_med==1 , cluster(section_id)
		 
			sum `x' [aweight=ihs_ratings_count] ///
		 if ihs_stars_med==1 & female==1, d
            local mean_f = r(mean)
            sum `x' [aweight=ihs_ratings_count] ///
		 if ihs_stars_med==1 & female==0, d
            local mean_m = r(mean)
			
			sum `x' [aweight=ihs_ratings_count] ///
		 if ihs_stars_med==1 , d
			local mean = r(mean)
			local sd = r(sd)
			
			sum femaleshare_female, d
			local p25 = r(p25)
			local p75 = r(p75)
			
			lincom femaleshare_female*`p75' - femaleshare_female*`p25'
			local estimate25_75 = r(estimate)
			local p_value_est25_75 = 2*ttail(r(df),abs(r(estimate)/r(se)))
			
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			
			eststo `x'_med, addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' ///
										p_value `p_value' estimate25_75 `estimate25_75' ///
										p_value_est25_75 `p_value_est25_75')
			estadd local classfe "Yes" , replace
			estadd local ind_fe "Yes" , replace
			
		eststo `x'_no_med: reg `x' femaleshare_male femaleshare_female i.female ///
		 i.Class##i.year_since_grad##i.female ///
			 i.top20##i.female ///
			 i.top20_m##i.female ///
			 pre_mgr2_v3##i.female  ///
			 pre_mgr2_v3_m##i.female  ///
			 c.pre_i_fin##i.female ///
			 i.pre_i_fin_m##i.female ///
			 c.m_pre_any_mgr_v3 ///
			 c.m_pre_mgr2_v3 ///
			 c.m_pre_i_fin ///
			 c.m_pre_i_cons ///
			 c.m_pre_i_oth ///
			 c.m_pre_PL2 ///
			 c.m_white ///
			 c.m_foreign ///
		 [aweight=ihs_ratings_count] ///
		 if ihs_stars_med==0 , cluster(section_id)
		 
			sum `x' [aweight=ihs_ratings_count] ///
		 if ihs_stars_med==0  & female==1, d
            local mean_f = r(mean)
            sum `x' [aweight=ihs_ratings_count] ///
		 if ihs_stars_med==0  & female==0, d
            local mean_m = r(mean)
			
			sum `x' [aweight=ihs_ratings_count] ///
		 if ihs_stars_med==0  , d
			local mean = r(mean)
			local sd = r(sd)
			
			sum femaleshare_female, d
			local p25 = r(p25)
			local p75 = r(p75)
			
			lincom femaleshare_female*`p75' - femaleshare_female*`p25'
			local estimate25_75 = r(estimate)
			local p_value_est25_75 = 2*ttail(r(df),abs(r(estimate)/r(se)))
			
			test femaleshare_male = femaleshare_female
			local p_value = r(p)
			
			eststo `x'_no_med, addscalars(mean `mean' mean_f `mean_f' mean_m `mean_m' sd `sd' ///
										p_value `p_value' estimate25_75 `estimate25_75' ///
										p_value_est25_75 `p_value_est25_75')
			estadd local classfe "Yes" , replace
			estadd local ind_fe "Yes" , replace		
		}
			
}

esttab  ihs_stars_med_mgr ihs_stars_no_med_mgr ihs_stars_med mgr2_v3_med mgr2_v3_no_med ///
	 using "${output}/tables/Table_5.tex", replace ///
	 keep(femaleshare_male femaleshare_female) se star(* .10 ** .05 *** .01) r2 ///
	 s(p_value mean_f mean_m r2 N classfe, ///
	 label("\(p\)-value Male vs. Female" "Female Mean" "Male Mean" "\(R^2\)" "N" ///
	 "Class x Year x Female FE") ///
     fmt(%9.3f %9.3f %9.3f %9.3g)) ///
	 label interaction(" $\times$ ") mtitles( ///
	 "\shortstack{Senior Manager \\ in Female-Friendly \\ Firms}" ///
	 "\shortstack{Senior Manager \\ in Non-Female-Friendly \\ Firms}" ///
	 "\shortstack{Working in \\ a Female-Friendly \\ Firm}"  ///
	 "\shortstack{Senior Manager \\ Restricted to \\ Female-Friendly \\ Firms}" ///
	 "\shortstack{Senior Manager \\ Restricted to \\ Non Female-Friendly \\ Firms}" ) ///
	 booktabs f noomitted nobaselevel
	 

	
}	
	
* Table 6: Effects of Female Peers on the Work Impacts of Children
if `p15'==1{
use "${data}/raw/survey/MBA_survey.dta", clear
lab var femaleshare "Female share"
 
//effect of children


forval i = 1/12{
	gen children_effect_dummy_`i' = children_effect_`i'==1
	replace children_effect_dummy_`i' = . if children_effect_`i'==.
	
	gen children_effect_work_dummy_`i' = children_effect_work_`i'==1
	replace children_effect_work_dummy_`i' = . if children_effect_work_`i'==.
	
}


egen any_children_effect = rowmax(children_effect_dummy_*)
egen any_children_effect_work = rowmax(children_effect_work_dummy_*)

eststo clear
local n = 1
cap drop p_value
gen p_value = .
foreach y in any_children_effect any_children_effect_work{
	eststo `y': areg `y' femaleshare,ab(Class) cluster(section_id)
	
	replace p_value = (2 * ttail(e(df_r), abs(_b[femaleshare] / _se[femaleshare]) ) ) if _n==`n'

	estadd local classfe "Yes" , replace
	sum `y'
	local mean = r(mean)
	local sd = r(sd)
	eststo `y', addscalars(mean `mean' sd `sd' )
	
	local n = `n' + 1

}

qqvalue p_value, method(simes) rank(rank) qvalue(qvalue)
local n = 1
foreach y in any_children_effect any_children_effect_work{
	levelsof qvalue if _n==`n',local(q)
	est restore `y'
	estadd scalar qvalue `q', replace
	
	local n = `n' + 1
}

esttab any_children_effect any_children_effect_work using  "${output}/tables/Table_6.tex", replace ///
 keep(femaleshare) se star(* .10 ** .05 *** .01) r2 ///
 s(classfe mean sd r2 qvalue N, ///
 label("Class FE" "Mean" "SD" "\(q\)-value" "\(R^2\)" "N")) ///
 label interaction(" $\times$ ") mtitles("\shortstack{Any Effects of \\ Children on \\ Work Choices}" ///
 "\shortstack{Any Adverse \\ Effects of \\ Children \\ at Work}") booktabs f noomitted nobaselevel	
}			
	
* Table 7: Probability of Entering the Same Firm
if `p16'==1{

* construct random assignment of sections

use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
keep if year_since_grad>=1 & year_since_grad<=15
bys linkedin_id: keep if _n==1
keep linkedin_id Class section_id

set seed 1000
forval iter = 1/1000{
	gen random = runiform()

	sort random
	gen randsection`iter' = .


	levelsof Class, local(classes)
	foreach c of local classes{
		preserve
		keep if Class == `c'

		
		levelsof section_id, local(sections)

		local i_min= 1
		foreach s of local sections{
			count if section_id ==`s' //keep the same section size as in kelly shue's paper
			local i_max = `i_min' + r(N) - 1
			replace randsection`iter' = `s' if _n >=`i_min'  & _n<=`i_max'
			local i_min = `i_max'+1
			
		}
		tempfile class`c'
		save `class`c''
		restore
		
		merge 1:1 linkedin_id using `class`c'', keep(1 3 4) update nogen
	}
	drop random 
}
save "${data}/analysis/random_sections.dta",replace


clear 
set obs 1000
gen iteration = _n 
gen same_section1 =. 
gen same_section_male1 =.
gen same_section_female1 =.


gen same_section_male2 =.
gen same_section_male_ff_firm2 =.

gen same_section_mixed2 =.
gen same_section_mixed_ff_firm2 =.

gen same_section_female2 =.
gen same_section_female_ff_firm2 =.
save "${data}/analysis/pairwise_saved_estimates.dta",replace



forval iter = 1/1000{
	use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta",clear
	merge m:1 linkedin_id using "${data}/analysis/random_sections.dta", keep(matched) nogen

	keep if year_since_grad >=1 & year_since_grad<=15

	keep linkedin_id female randsection`iter' Class firm_id_LI year year_since_grad  ihs_stars_med 

	preserve
	rename * *_1
	rename Class_1 Class
	rename year_1 year
	tempfile switches
	save `switches'
	restore

	rename * *_2
	rename Class_2 Class
	rename year_2 year
	tempfile nonswitches
	save `nonswitches', replace

	joinby Class year using `switches'
	drop if linkedin_id_1==linkedin_id_2

	gen samefirm = (firm_id_LI_1==firm_id_LI_2)
	replace samefirm=. if firm_id_LI_1=="" | firm_id_LI_2==""

	gen same_section = (randsection`iter'_1==randsection`iter'_2)
	replace same_section=. if randsection`iter'_1==. | randsection`iter'_2==.

	gen same_gender = (female_1==female_2)
	replace same_gender=. if female_1==. | female_2==.


	gen both_female = female_1==1 & female_2==1
	gen both_male = female_1==0 & female_2==0

	gen same_section_1 = same_section==1 & female_1==0 & female_2==0
	gen same_section_2 = same_section==1 & ((female_1==1 & female_2==0 ) | (female_1==0 & female_2==1 ))
	gen same_section_3 = same_section==1 & female_1==1 & female_2==1



	local variables samefirm  
	local h ihs_stars_med_1
	foreach y of local variables{

		eststo A: qui reghdfe `y' same_section##both_male same_section##both_female  i.female_1 i.female_2 , ab(firm_id_LI_1 firm_id_LI_2 Class year i.Class#i.year)
		
		
		eststo B: qui reghdfe `y' same_section_1##i.`h' same_section_2##i.`h' same_section_3##i.`h' i.female_1##i.`h' i.female_2##i.`h' ,  ab(firm_id_LI_1 firm_id_LI_2 female_1 Class year i.Class#i.year)

	}

	use "${data}/analysis/pairwise_saved_estimates.dta",clear

	est restore A
	replace same_section1 = _b[1.same_section] if iteration==`iter'
	replace same_section_male1 = _b[1.same_section#1.both_male] if iteration==`iter'
	replace same_section_female1 = _b[1.same_section#1.both_female] if iteration==`iter'

	est restore B
	replace same_section_male2 = _b[1.same_section_1] if iteration==`iter'
	replace same_section_male_ff_firm2 = _b[1.same_section_1#1.ihs_stars_med_1] if iteration==`iter'

	replace same_section_mixed2 = _b[1.same_section_2] if iteration==`iter'
	replace same_section_mixed_ff_firm2 = _b[1.same_section_2#1.ihs_stars_med_1] if iteration==`iter'

	replace same_section_female2 = _b[1.same_section_3] if iteration==`iter'
	replace same_section_female_ff_firm2 = _b[1.same_section_3#1.ihs_stars_med_1] if iteration==`iter'


	save "${data}/analysis/pairwise_saved_estimates.dta", replace
	display("iteration `iter' saved")
}



* get actual estimates
use "${data}/raw/LI_workhistory_yearly_educ_US_gender_fake.dta", clear
keep if year_since_grad >=1 & year_since_grad<=15

keep linkedin_id female section_id Class firm_id_LI year year_since_grad  ihs_stars_med 

preserve
rename * *_1
rename Class_1 Class
rename year_1 year
tempfile switches
save `switches'
restore

rename * *_2
rename Class_2 Class
rename year_2 year
tempfile nonswitches
save `nonswitches', replace


joinby Class year using `switches'
drop if linkedin_id_1==linkedin_id_2

gen samefirm = (firm_id_LI_1==firm_id_LI_2)
replace samefirm=. if firm_id_LI_1=="" | firm_id_LI_2==""

gen same_section = (section_id_1==section_id_2)
replace same_section=. if section_id_1==. | section_id_2==.

gen same_gender = (female_1==female_2)
replace same_gender=. if female_1==. | female_2==.


gen both_female = female_1==1 & female_2==1
gen both_male = female_1==0 & female_2==0

gen same_section_1 = same_section==1 & female_1==0 & female_2==0
gen same_section_2 = same_section==1 & ((female_1==1 & female_2==0 ) | (female_1==0 & female_2==1 ))
gen same_section_3 = same_section==1 & female_1==1 & female_2==1



local variables samefirm  
local h ihs_stars_med_1
foreach y of local variables{

	eststo A: qui reghdfe `y' same_section##both_male same_section##both_female  i.female_1 i.female_2 , cluster(linkedin_id_1 linkedin_id_2) ab(firm_id_LI_1 firm_id_LI_2 Class year i.Class#i.year)
	sum `y' if female_1==1,d
	local mean_f = r(mean)
	sum `y' if female_1==0,d
	local mean_m = r(mean)
	estadd scalar mean_f = `mean_f'
	estadd scalar mean_m = `mean_m'
	estadd local firmfe "Yes" , replace
	estadd local classfe "Yes" , replace
	estadd local yearfe "Yes" , replace
	estadd local cluster "Person-Person" , replace
	test 1.same_section#1.both_male = 1.same_section#1.both_female
	estadd scalar p_value = r(p)
	
	eststo B: qui reghdfe `y' same_section_1##i.`h' same_section_2##i.`h' same_section_3##i.`h' i.female_1##i.`h' i.female_2##i.`h' , /// 
				cluster(linkedin_id_1 linkedin_id_2) ab(firm_id_LI_1 firm_id_LI_2 female_1 Class year i.Class#i.year)
		sum `y' if female_1==1,d
		local mean_f = r(mean)
		sum `y' if female_1==0,d
		local mean_m = r(mean)
		estadd scalar mean_f = `mean_f'
		estadd scalar mean_m = `mean_m'
		estadd local firmfe "Yes" , replace
		estadd local classfe "Yes" , replace
		estadd local yearfe "Yes" , replace
		estadd local cluster "Person-Person" , replace
		test 1.same_section_1#1.`h' = 1.same_section_3#1.`h'
		estadd scalar p_value = r(p)
}


use "${data}/analysis/pairwise_saved_estimates.dta", clear
est restore A
gen same_section1_t = _b[1.same_section] 
gen same_section_male1_t = _b[1.same_section#1.both_male] 
gen same_section_female1_t = _b[1.same_section#1.both_female] 

est restore B
gen same_section_male2_t = _b[1.same_section_1] 
gen same_section_male_ff_firm2_t = _b[1.same_section_1#1.ihs_stars_med_1] 

gen same_section_mixed2_t = _b[1.same_section_2] 
gen same_section_mixed_ff_firm2_t = _b[1.same_section_2#1.ihs_stars_med_1] 

gen same_section_female2_t = _b[1.same_section_3] 
gen same_section_female_ff_firm2_t = _b[1.same_section_3#1.ihs_stars_med_1] 

foreach k in same_section1 same_section_male1 same_section_female1 same_section_male2 same_section_male_ff_firm2 same_section_mixed2 same_section_mixed_ff_firm2 same_section_female2 same_section_female_ff_firm2{
	cap drop aux_`k'
	gen aux_`k' = (abs(`k')>=abs(`k'_t))
}

*p value 
mat ri = (0,0,0)
mat colnames ri= "1.same_section" "1.same_section#1.both_male" "1.same_section#1.both_female"

sum aux_same_section1 
mat ri[1,1] = r(mean)

sum aux_same_section_male1 
mat ri[1,2] = r(mean)

sum aux_same_section_female1 
mat ri[1,3] = r(mean)



*CREATE REDUNDANT MATRIX WITH UNIQUE ENTRIES
mat end= (1, 1, 1)
mat colnames end= "1.same_section" "1.same_section#1.both_male" "1.same_section#1.both_female"

*ESTADD MATRICES
est restore A
estadd matrix myri= ri, replace
estadd matrix end= end, replace



 
 
 
*CUSTOM CIs
mat ri = (0,0,0,0,0,0)
mat colnames ri= "1.same_section_1" "1.same_section_1#1.ihs_stars_med_1" "1.same_section_2" "1.same_section_2#1.ihs_stars_med_1" "1.same_section_3" "1.same_section_3#1.ihs_stars_med_1"

sum aux_same_section_male2 
mat ri[1,1] = r(mean)

sum aux_same_section_male_ff_firm2 
mat ri[1,2] = r(mean)

sum aux_same_section_mixed2 
mat ri[1,3] = r(mean)

sum aux_same_section_mixed_ff_firm2
mat ri[1,4] = r(mean)

sum aux_same_section_female2
mat ri[1,5] = r(mean)

sum aux_same_section_female_ff_firm2 
mat ri[1,6] = r(mean)



*CREATE REDUNDANT MATRIX WITH UNIQUE ENTRIES
mat end= (1, 1, 1, 1, 1, 1)
mat colnames end= "1.same_section_1" "1.same_section_1#1.ihs_stars_med_1" "1.same_section_2" "1.same_section_2#1.ihs_stars_med_1" "1.same_section_3" "1.same_section_3#1.ihs_stars_med_1"

*ESTADD MATRICES
est restore B
estadd matrix myri= ri, replace
estadd matrix end= end, replace

 
local y samefirm
local h ihs_stars_med_1

 
local y samefirm
local h ihs_stars_med_1


esttab A B  using "${output}/tables/Table_7.tex", replace ///
	cells(b(star fm(a3)) se(par fmt(a3)) myri(par("{[}") fmt(%9.3f))&end(par("{]}"))) substitute (" {]}1" "{]}")  ///
	rename(1.same_section#1.both_male 1.same_section_1 1.same_section#1.both_female 1.same_section_3 1.same_section 1.same_section_2) ///
	 keep( 1.same_section_1 1.same_section_1#1.`h'  1.same_section_2 1.same_section_2#1.`h'  1.same_section_3 1.same_section_3#1.`h') se star(* .10 ** .05 *** .01) r2 ///
	 coeflabel( 1.same_section_1 "Same Section \(\times\) Both Males" ///
		1.same_section_1#1.`h' "Same Section  \(\times\) Both Males \(\times\) Female-Friendly Firm" ///
		1.same_section_2 "Same Section \(\times\) Mixed Gender" ///
		1.same_section_2#1.`h' "Same Section  \(\times\) Mixed Gender  \(\times\) Female-Friendly Firm" ///
		1.same_section_3 "Same Section \(\times\) Both Females" ///
		1.same_section_3#1.`h' "Same Section \(\times\) Both Females  \(\times\) Female-Friendly Firm") ///
	 s(p_value mean_f mean_m  r2 N classfe firmfe, ///
	 label("\(p\)-value Both Male vs. Both Female" "Female Mean"  "Male Mean"  ///
	  "\(R^2\)" "N" "Class x Year FE" "Firm FE")  ///
	 fmt( %9.4g  %9.4g %9.4g %9.3f %12.0gc )) collabel(none) ///
	 label interaction(" $\times$ ") nomtitles booktabs f noomitted nobaselevel


}


